/**
 * Called by the Android System after an Android user was removed.
 *
 * The HAL can use this property to remove its equivalent user.
 *
 * This is write-only call - the Android System is not expecting a reply from the HAL. Hence,
 * this request should not fail - if the equivalent HAL user cannot be removed, then HAL should
 * mark it as inactive or recover in some other way.
 *
 * The request is made by setting the VehiclePropValue with the contents defined by
 * RemoveUserRequest.
 *
 * For example, if system had 3 users (0, 10, and 11) and user 11 was removed, the request
 * would be:
 *
 * int32[0]: 42  // request id
 * int32[1]: 11  // (Android user id of the removed user)
 * int32[2]: 0   // (Android user flags of the removed user)
 * int32[3]: 10  // current user
 * int32[4]: 0   // current user flags (none)
 * int32[5]: 2   // number of users
 * int32[6]: 0   // 1st user (user 0)
 * int32[7]: 0   // 1st user flags (none)
 * int32[8]: 10  // 2nd user (user 10)
 * int32[9]: 0   // 2nd user flags (none)
 *
 * @change_mode VehiclePropertyChangeMode:STATIC
 * @access VehiclePropertyAccess:WRITE
 */

#include "PlatformProperty.h"
#include "UserHalHelper.h"

using namespace ecarx::vhal::core::property;
using namespace user_hal_helper;

SYSTEM_WRITE_PROPERTY(REMOVE_USER, MixedValue) {
    builder.area(VehicleArea::GLOBAL)
        .immutableStatus(PropertyStatus::Active)
        .onSetProperty([this](const MixedValue &value) {
            auto removeUserRequest = toRemoveUserRequest(value);
            if (!removeUserRequest.ok()) {
                ALOGE("REMOVE_USER: toRemoveUserRequest failed: %s", removeUserRequest.error().message().c_str());
                return;
            }
            if (removeUserRequest->requestId == INVALID_REQUEST_ID) {
                ALOGE("REMOVE_USER: removeUserRequest requestId is invalid");
                return;
            }
            ALOGD("REMOVE_USER: removeUserRequest:= %s", toString(removeUserRequest.value()).c_str());

            std::string stringPidToUids = this->getPlatformProfile()->getAuxiliary()->getString(PROFILE_ID_TO_USER_ID, "");
            UserInfo removedUserInfo = removeUserRequest->removedUserInfo;

            int profileId = getProfileId(stringPidToUids, removedUserInfo.userId);
            auto flags = (int32_t)removedUserInfo.flags & ~(int32_t)UserFlags::DISABLED;
            removedUserInfo.flags = static_cast<UserFlags>(flags);
            if (isOwnerUser(removedUserInfo)) { // 删除车主用户
                if (profileId == OWNER_PROFILE_ID
                    && this->getPlatformProfile()->isCreated(OWNER_PROFILE_ID)) { // 已有profile_1和userId映射
                    this->getPlatformProfile()->deleteProfile(OWNER_PROFILE_ID);
                    stringPidToUids = removeRelationPidToUidsString(OWNER_PROFILE_ID, stringPidToUids);
                } else {
                    ALOGE("REMOVE_USER: no owner profile for userId=%d", removedUserInfo.userId);
                }
            } else if (isEphemeralUser(removedUserInfo)) { // 删除临时用户
                if (profileId == EPHEMERAL_PROFILE_ID) { // 已有profile_12和userId映射
                    stringPidToUids = removeRelationPidToUidsString(EPHEMERAL_PROFILE_ID, stringPidToUids);
                } else {
                    ALOGE("REMOVE_USER: no ephemeral profile for userId=%d", removedUserInfo.userId);
                }
            } else if (isGuestUser(removedUserInfo)) { // 删除游客用户
                if (profileId == GUEST_PROFILE_ID) { // 已有profile_13和userId映射
                    stringPidToUids = removeRelationPidToUidsString(GUEST_PROFILE_ID, stringPidToUids);
                } else {
                    ALOGE("REMOVE_USER: no ephemeral profile for userId=%d", removedUserInfo.userId);
                }
            } else if (isFamilyUser(removedUserInfo)) { // 删除亲情用户
                if (profileId != INVALID_PROFILE_ID
                    && this->getPlatformProfile()->isCreated(profileId)) { // 已有profile_2~6和userId映射
                    this->getPlatformProfile()->deleteProfile(profileId);
                    stringPidToUids = removeRelationPidToUidsString(profileId, stringPidToUids);
                } else {
                    ALOGE("REMOVE_USER: no family profile for userId=%d", removedUserInfo.userId);
                }
            } else { // 无效的用户标志位
                ALOGE("REMOVE_USER: invalid userFlags for userId=%d", removedUserInfo.userId);
            }

            this->getPlatformProfile()->getAuxiliary()->setString(PROFILE_ID_TO_USER_ID, stringPidToUids);
            this->getPlatformProfile()->getAuxiliary()->save();
        });
}